#include "background.h"
#include <QGraphicsScene>
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

Background::Background(const QString& imagePath, qreal speed)
    : m_speed(speed) {
    QPixmap bg(imagePath);
    if(bg.isNull()) {
        bg = QPixmap(800, 600);
        bg.fill(Qt::darkGray);
    }
    setPixmap(bg);
    setPos(0, 0);
    setZValue(0);
}

void Background::updatePosition(Background* partner) {
    // 向左移动
    setX(x() - m_speed);

    // 当自己完全移出屏幕时，重置到右侧
    if(x() + pixmap().width() < 0) {
        resetPosition(partner);
    }
}

void Background::resetPosition(Background* leadBg) {
    setX(leadBg->x() + leadBg->pixmap().width() - 5); // 轻微重叠避免缝隙
    isLeading = !isLeading; // 切换领导状态
}
